package main.model;

import java.util.Collections;
import java.util.Comparator;
import java.util.Iterator;
import java.util.SortedSet;
import java.util.TreeSet;
import main.controller.GEDIVAController;


/**
 * A Model for storing stock information about a particular company.
 *
 * Special note: This class implements Iterable<Day> by extension from
 * DataModel. So it can be used as:
 * for (Day d : someCompany) { ... }
 *
 * @author Michele Reshef, Kevin Gao
 *
 */
public class Company extends DataModel {

    // Use sorted set so that data points can be arbitrarily added and sorted by
    // date (by default).
    private final SortedSet<Day> myRows;

    /**
     * Default constructor for company w/ empty set of data.
     *
     * @param controller The controller associated with the model
     */
    public Company (GEDIVAController controller) {
        super(controller);
        myRows = new TreeSet<Day>();
    }

    @Override
    public Day getMaxRow () {
        if (isEmpty()) {
            return null;
        }
        return Collections.max(myRows, new Comparator<Day>() {
            @Override
            public int compare (Day d1, Day d2) {
                return (int) (d1.getHigh() - d2.getHigh());
            }
        });
    }

    @Override
    public Day getMinRow () {
        if (isEmpty()) {
            return null;
        }
        return Collections.min(myRows, new Comparator<Day>() {
            @Override
            public int compare (Day d1, Day d2) {
                return (int) (d1.getLow() - d2.getLow());
            }
        });
    }

    @Override
    public Day getFirstRow () {
        return myRows.first();
    }

    @Override
    public Day getLastRow () {
        return myRows.last();
    }

    @Override
    public int getSizeOfData () {
        return myRows.size();
    }

    @Override
    public void addRow (Day d) {
        myRows.add(d);
    }

    @Override
    public Iterator<Day> iterator () {
        return myRows.iterator();
    }

    @Override
    public boolean isEmpty () {
        return myRows.isEmpty();
    }


}
